package lex.fsm;

import java.util.Arrays;
import java.util.EventObject;

public class StateEvent extends EventObject {
	public static final String WILDCARD_EVENT_ID = "*";
	private static final Object[] EMPTY = new Object[0];
	private final StateContext context;
	private final Object[] arguments;

	/**
	 * Creates a new {@link StateEvent} with the specified id and no arguments.
	 * 
	 * @param id
	 *            the event id.
	 * @param context
	 *            the {@link StateContext} the event was triggered for.
	 */
	public StateEvent(StateContext context) {
		this(context, EMPTY);
	}

	/**
	 * Creates a new {@link StateEvent} with the specified id and arguments.
	 * 
	 * @param id
	 *            the event id.
	 * @param context
	 *            the {@link StateContext} the event was triggered for.
	 * @param arguments
	 *            the event arguments.
	 */
	public StateEvent(StateContext context, Object[] arguments) {
		super(context.getCurrentState());
		if (arguments == null)
			arguments = EMPTY;
		this.context = context;
		this.arguments = arguments;
	}

	public StateContext getContext() {
		return context;
	}

	public State getSource() {
		return (State) super.getSource();
	}

	/**
	 * Returns the arguments of this {@link StateEvent}.
	 * 
	 * @return the arguments. Returns an empty array if this {@link StateEvent}
	 *         has no arguments.
	 */
	public Object[] getArguments() {
		return arguments;
	}

	public String toString() {
		StringBuilder buf = new StringBuilder();
		buf.append("source=").append(this.getSource());
		buf.append(", arguments=").append(Arrays.toString(arguments));
		return buf.toString();
	}
}
